﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZoneLinePacking.Core.Helpers
{
    /// <summary>
    /// 验证辅助类
    /// </summary>
    public class ValidationHelper
    {
        /// <summary>
        /// 属性的数据验证
        /// </summary>
        public static string ValidateProperty<T>(T obj, string propertyName)
        {
            var vc = new ValidationContext(obj, null, null);
            vc.MemberName = propertyName;
            var res = new List<ValidationResult>();
            var result = Validator.TryValidateProperty(obj.GetType().GetProperty(propertyName).GetValue(obj, null), vc, res);
            if (res.Count > 0)
            {
                return string.Join(Environment.NewLine, res.Select(r => r.ErrorMessage).ToArray());
            }
            return string.Empty;
        }
    }
}
